<div class="filter">
    <div class="item">搜索：</div>
    <div class="item f0">
        <select>
            <option value="uid">账号</option>
            <option value="name">真实姓名</option>
        </select>
    </div>

    <div class="f1 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="" />
    </div>
    <div class="f7 item">
        <select data-am-selected>
            <option value="-1">全部状态</option>
            <option value="0">正常</option>
            <option value="1">冻结</option>
            <option value="2">停用</option>
        </select>
    </div>
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
    <div class=".f100 item">
        <button class="btn-add-account am-btn am-btn-primary">添加管理账号</button>
    </div>
    <div class="clear"></div>
</div>

<div class="table">
    <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact" id="result-table">
        <thead>
        <tr>
            <th> </th>
            <th>用户名</th>
            <th>姓名</th>
            <th>用户组</th>
            <th>状态</th>
            <th>QQ</th>
            <th>微信</th>
            <th>手机号</th>
            <th>IP</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <div class="zxf_pagediv"></div>
    <div style="height: 150px;"></div>
</div>

<style type="text/css">
    .am-checkbox {
        margin: 0px;
        line-height: 16px;
        padding-left: 16px;
    }
    .am-dropdown .am-btn {
        font-size: 12px;
        line-height: 18px;
    }
</style>

<script>
    Util.restorePageFilter();
    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        // 检索数据
        var data = {
            minGrade: 50, 
            maxGrade: 98
        };
        var state = $(".f7 select").val();
        if (state != -1) date.state = state;
        var unknown = $(".f1 input").val().trim();
        if (unknown) {
            data[$(".f0 select").val()] = unknown;
        }
        Util.savePageFilter([".f7 select", ".f1 input", ".f0 select"])

        startLoading();
        CMD.get_user_list(data, page, size, function(r) {
            stopLoading();
            cb(r);
        });
    }, function(table, list, sum) {
        var ips = [];
        for (var i in list) {
            var d = list[i];
            var tr = $("<tr></tr>");
            if (d.online == 1)
                tr.append('<td><i class="am-icon-circle" style="color:blue"></i></td>');
            else
                tr.append('<td><i class="am-icon-circle" style="color:gray"></i></td>');

            tr.append("<td class='name'>" + d.mobile + "</td>");
            tr.append("<td>" + (d.name || '') + "</td>");
            tr.append("<td>" + (Rights.roles[d.grade || 1] || "超级管理员") + "</td>");
            if (d.state == 1) tr.append("<td>冻结</td>");
            else if (d.state == 2) tr.append("<td>停用</td>");
            else tr.append("<td>正常</td>");
            tr.append("<td>" + (d.qq || '') + "</td>");
            tr.append("<td>" + (d.wechat || '') + "</td>");
            tr.append("<td>" + (d.phone || '') + "</td>");
            tr.append("<td class='ip'>" + (d.ip || '') + "</td>");

            // 操作按钮
            var oper = '<button class="edit" value="__v__">修改</button>\
            <button class="offline" value="__v__">踢下线</button>\
            <button class="ips" value="__v__">IP绑定</button>\
            <button class="delete" value="__v__">删除</button>\
            ';
            oper = oper.replace(/__v__/g, d.mobile);
            tr.append("<td>" + oper + "</td>");
            if (d.ip && ips.indexOf(d.ip) == -1) {
                ips.push(d.ip);
            }
            table.append(tr);
        }

        var _findUser = function(uid) {
            for (var i in list) {
                if (list[i].mobile == uid) return list[i];
            }
        }
        $("td button.edit").click(function(e) {
            // 编辑用户信息
            context.center.user = _findUser($(e.currentTarget).attr("value"));
            context.center.adminOper = 'edit';
            parent._f = _f;
            Util.addPopUp("modify_admin_account", "modify_admin_account", 500);
        });
        $("td button.offline").click(function(e) {
            var u = _findUser($(e.currentTarget).attr("value"));
            if (!confirm("确定将管理员" + u.mobile + "踢下线？")) return;
            startLoading();
            CMD.offline(u.id, function() {
                stopLoading();
                _f();
            });
        })
        $("td button.delete").click(function(e) {
            var u = _findUser($(e.currentTarget).attr("value"));
            if (!confirm("确定将管理员" + u.mobile + "删除？")) return;
            startLoading();
            CMD.user_state(u.id, 2, function() {
                CMD.offline(u.id, function() {
                    stopLoading();
                    _f();
                });
            });
        });
        $("td button.ips").click(function(e) {
            // 编辑用户信息
            context.center.user = _findUser($(e.currentTarget).attr("value"));
            parent._f = _f;
            Util.addPopUp("modify_admin_ips", "modify_admin_ips", 500);
        });

        // 查询IP信息
        for (var i in ips) {
            loopUpIp(ips[i]);
        }
    });

    function loopUpIp(ip) {
        var td = $("td.ip");
        CMD.ip_info(ip, function(r) {
            for (var i = 0; i < td.length; i++) {
                if ($(td[i]).html() != ip) continue;
                $(td[i]).html(ip + "(" + r.addr + ")");
            }
        });
    }

    $("button.btn-add-account").click(function() {
        parent._f = _f;
        Util.addPopUp("add_account", "add_account", 330);
    })
</script>
